/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__do_commit.c                                     */
/*  NOTE      =                                                    */
/*  DATE      = 2014/12/04 by geyh                                 */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>
#include <protobuf-c/protobuf-c.h>

#include "frcomm.h"
#include "frmem.h"
#include "frame.h"
#include "client.pb-c.h"
#include "frsutl.h"
#include "frcmd_server.h"
#include "apmtest_cunit.h"

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}
FR_STATIC int __protobuf_do_pack(Command *p_st_cmd, char **pp_data, unsigned int *p_ui_out)
{
    unsigned int ui_len = 0;
    void *p_out = NULL;

    if (p_st_cmd == NULL)
        return FR_FAIL;

    ui_len = command__get_packed_size(p_st_cmd);
    p_out = malloc(ui_len);
    if (p_out == NULL)
        return FR_FAIL;

    command__pack(p_st_cmd, p_out);
    *p_ui_out = ui_len;
    *pp_data = p_out;

    return FR_SUCC;
}


int __do_commit(Command *p_st_cmd);



void case1(void)
{
    int i_ret = 0;
    Command *p_st_cmd = NULL;
    CommitCommand *commit_cmd = NULL;
    char *p_cmd = NULL;
    unsigned int i_cmd_len = 0;
    char *p_result = NULL;
    int i_size_tmp = 0;
    int accept_sock = 1;

    //设置commit_cmd
    commit_cmd = malloc(sizeof(CommitCommand));
    commit_command__init(commit_cmd);
    commit_cmd->action = 3;
    commit_cmd->cmd = COMMIT_COMMAND__TYPE__MODULE;

    //设置p_st_cmd
    p_st_cmd = malloc(sizeof(Command));
    command__init(p_st_cmd);
    p_st_cmd->commit_cmd = commit_cmd;
    p_st_cmd->cmd_type = COMMAND__COMMAND_TYPE__Type_CommitCommand;

    i_ret = __protobuf_do_pack(p_st_cmd,&p_cmd,&i_cmd_len);
    if(i_ret != FR_SUCC)
    {
        free(commit_cmd);
        free(p_st_cmd);

    }

    i_ret = __do_command(p_cmd,i_cmd_len,&p_result, accept_sock);
    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    
    command__free_unpacked(p_st_cmd, NULL);
    free(p_cmd);
    return;
}

void case2(void)
{
    int i_ret = 0;
    Command *p_st_cmd = NULL;
    CommitCommand *commit_cmd = NULL;
    char *p_cmd = NULL;
    unsigned int i_cmd_len = 0;
    char *p_result = NULL;
    int i_size_tmp = 0;
    int accept_sock = 1;

    //设置commit_cmd
    commit_cmd = malloc(sizeof(CommitCommand));
    commit_command__init(commit_cmd);
    commit_cmd->action = COMMIT_COMMAND__ACTION__Commit;
    commit_cmd->cmd = 0;

    //设置p_st_cmd
    p_st_cmd = malloc(sizeof(Command));
    command__init(p_st_cmd);
    p_st_cmd->commit_cmd = commit_cmd;
    p_st_cmd->cmd_type = COMMAND__COMMAND_TYPE__Type_CommitCommand;

    i_ret = __protobuf_do_pack(p_st_cmd,&p_cmd,&i_cmd_len);
    if(i_ret != FR_SUCC)
    {
        free(commit_cmd);
        free(p_st_cmd);

    }

    i_ret = __do_command(p_cmd,i_cmd_len,&p_result, accept_sock);
    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    command__free_unpacked(p_st_cmd, NULL);
    free(p_cmd);
    return;
}


void case3(void)
{
    int i_ret = 0;
    Command *p_st_cmd = NULL;
    CommitCommand *commit_cmd = NULL;
    char *p_cmd = NULL;
    unsigned int i_cmd_len = 0;
    char *p_result = NULL;
    int i_size_tmp = 0;
    int accept_sock = 1;

    //设置commit_cmd
    commit_cmd = malloc(sizeof(CommitCommand));
    commit_command__init(commit_cmd);
    commit_cmd->action = COMMIT_COMMAND__ACTION__Commit;
    commit_cmd->cmd = COMMIT_COMMAND__TYPE__MODULE;

    //设置p_st_cmd
    p_st_cmd = malloc(sizeof(Command));
    command__init(p_st_cmd);
    p_st_cmd->commit_cmd = commit_cmd;
    p_st_cmd->cmd_type = COMMAND__COMMAND_TYPE__Type_CommitCommand;

    i_ret = __protobuf_do_pack(p_st_cmd,&p_cmd,&i_cmd_len);
    if(i_ret != FR_SUCC)
    {
        free(commit_cmd);
        free(p_st_cmd);

    }

    i_ret = __do_command(p_cmd,i_cmd_len,&p_result, accept_sock);
    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    command__free_unpacked(p_st_cmd, NULL);
    free(p_cmd);
    return;
}


void case4(void)
{
    int i_ret = 0;
    Command *p_st_cmd = NULL;
    CommitCommand *commit_cmd = NULL;
    char *p_cmd = NULL;
    unsigned int i_cmd_len = 0;
    char *p_result = NULL;
    int i_size_tmp = 0;
    int accept_sock = 1;

    //设置commit_cmd
    commit_cmd = malloc(sizeof(CommitCommand));
    commit_command__init(commit_cmd);
    commit_cmd->action = COMMIT_COMMAND__ACTION__Rollback;
    commit_cmd->cmd = COMMIT_COMMAND__TYPE__MODULE;

    //设置p_st_cmd
    p_st_cmd = malloc(sizeof(Command));
    command__init(p_st_cmd);
    p_st_cmd->commit_cmd = commit_cmd;
    p_st_cmd->cmd_type = COMMAND__COMMAND_TYPE__Type_CommitCommand;

    i_ret = __protobuf_do_pack(p_st_cmd,&p_cmd,&i_cmd_len);
    if(i_ret != FR_SUCC)
    {
        free(commit_cmd);
        free(p_st_cmd);

    }

    i_ret = __do_command(p_cmd,i_cmd_len,&p_result, accept_sock);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    command__free_unpacked(p_st_cmd, NULL);
    free(p_cmd);
    return;
}

int add_suite()
{
	CU_pSuite pSuite = NULL;

	pSuite = CU_add_suite("t__do_commit 测试", __suite_init, __suite_cleanup);

	if (NULL == pSuite)
	{
		return 1;
	}


    //增加测试用例
   	CU_add_test(pSuite, "case1", case1);

    CU_add_test(pSuite, "case2", case2);

    CU_add_test(pSuite, "case3", case3);

    CU_add_test(pSuite, "case4", case4);

    return 0;

}

int main(int argc, char **argv)
{
    fr_slab_init(52428800, "test");
    run_cunit(add_suite);
    fr_slab_destroy();

    return 0;
}
